package com.ljf.eslab.lab.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

import com.ljf.common.utils.Query;
import com.ljf.eslab.lab.annotation.NeedSid;
import com.ljf.eslab.lab.dto.CourseAndTeacherDTO;
import com.ljf.eslab.lab.entity.CourseEntity;
import com.ljf.eslab.lab.service.CourseService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.ljf.eslab.lab.entity.StuCourseEntity;
import com.ljf.eslab.lab.service.StuCourseService;
import com.ljf.common.utils.PageUtils;
import com.ljf.common.utils.R;

import javax.servlet.http.HttpServletRequest;


/**
 * @author ljf
 * @email ljf@gmail.com
 * @date 2021-07-08 11:23:39
 */
@Slf4j
@RestController
@RequestMapping("lab/stucourse")
public class StuCourseController {
    @Autowired
    private StuCourseService stuCourseService;
    @Autowired
    private CourseService courseService;

    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        PageUtils page = stuCourseService.queryPage(params);

        return R.ok().put("page", page);
    }

    /**
     * 信息
     */
    @RequestMapping("/info/{id}")
    public R info(@PathVariable("id") Integer id) {
        StuCourseEntity stuCourse = stuCourseService.getById(id);

        return R.ok().put("stuCourse", stuCourse);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody StuCourseEntity stuCourse) {
        stuCourseService.save(stuCourse);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    public R update(@RequestBody StuCourseEntity stuCourse) {
        stuCourseService.updateById(stuCourse);

        return R.ok();
    }
    /**
     * 列出当前登录学生的没有选过的课程
     *
     * @param request
     * @return
     */
    @NeedSid
    @RequestMapping("/listCoursesBySID")
    public R listCoursesBySID(@RequestParam Map<String, Object> params ,HttpServletRequest request) {
        Integer sid = (Integer) request.getAttribute("sid");
        //1.选出可选课程
        PageUtils page = stuCourseService.queryCoursesPageBySid(new Query<CourseAndTeacherDTO>().getPage(params),sid);
        return R.ok().put("page", page);
    }

    /**
     * 列出当前登录学生的所有选课记录
     *
     * @param request
     * @return
     */
    @NeedSid
    @RequestMapping("/listBySID")
    public R infoInHeader(@RequestParam Map<String, Object> params ,HttpServletRequest request) {
        Integer sid = (Integer) request.getAttribute("sid");
        //1.选出可选课程
        PageUtils page = stuCourseService.queryPageBySid(new Query<CourseAndTeacherDTO>().getPage(params),sid);
        return R.ok().put("page", page);
    }

    /**
     * 批量选课
     *
     * @param cids 选择的课程的ids
     * @return
     */
    @NeedSid
    @RequestMapping("/select")
    public R select(@RequestBody Integer[] cids, HttpServletRequest request) {
        if (cids == null || cids.length == 0) {
            return R.error("未选中任何课程");
        }
        log.debug("要退选的课程数：{}",cids.length);
        //获取当前学生id
        Integer sid = (Integer) request.getAttribute("sid");
        //1.在课程表中批量修改课程的学生数，使课程的ids内的所有课程stu_count++
        log.debug("在课程表中批量修改课程的学生数，使课程的ids内的所有课程stu_count++");
        Boolean isSuccess = courseService.increaseByCids(cids);
        //2.在选课表中，根据当前userId下的stuId批量插入选课记录，需要关联课程表
        log.debug("在选课表中，根据当前userId下的stuId批量插入选课记录，需要关联课程表");
        stuCourseService.insertByCids(sid, Arrays.asList(cids));
        //3.如果选满则失败，未溢出则成功
        if (isSuccess)
            return R.ok();
        else return R.error("课程可选人数已满");
    }

    /**
     * 批量退课
     *
     * @param ids 选课记录ids
     * @return
     */
    @RequestMapping("/delete")
    public R delete(@RequestBody Integer[] ids) {
        if (ids == null || ids.length == 0) {
            return R.error("未选中任何课程");
        }
        log.debug("从选课记录ids获取要退选的课程的cids");
        //从选课记录ids获取要退选的课程的cids
        List<Integer> cids = stuCourseService.selectByIds(Arrays.asList(ids)).stream().map(StuCourseEntity::getCourseId).collect(Collectors.toList());
        log.debug("先--选中的课程学生数");
        courseService.decreaseStuByIds(cids);
        log.debug("再删除这些选课记录");
        stuCourseService.removeByIds(Arrays.asList(ids));
        return R.ok();
    }

}
